package com.hongdee.crm.loss.service.impl;

import com.hongdee.common.dao.IHibernateDao;
import com.hongdee.common.util.TokenUtil;
import com.hongdee.crm.loss.dao.ITcrmLossDao;
import com.hongdee.crm.loss.entity.TCrmCarLoss;
import com.hongdee.crm.loss.service.TCrmLossService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dell on 2017/5/5.
 */
@Service
public class TCrmLossServiceImpl implements TCrmLossService {
    @Autowired
    private ITcrmLossDao iTcrmLossDao;
    private String tabName = "t_crm_car_loss";
    @Autowired
    private IHibernateDao iHibernateDao;


    /*
    * 停用启用
    * */
    @Override
    public void update(String id, String status) throws Exception {
        String hql = "update TCrmCarLoss set status = :status where id = :id ";
        Map params = new HashMap();
        params.put("id", id);
        params.put("status", status);
        this.iTcrmLossDao.update(hql, params);
    }

    /*
    * 新增与保存
    * */
    @Override
    public TCrmCarLoss saveOrUpdate(TCrmCarLoss tem, String uid) throws Exception {
        //根据显示编码 显示名称去重
        if (null == tem.getId() || StringUtils.isEmpty(tem.getId())) {
            //新增时判断是否已经存在
            if (isExist(tem.getCode(), tem.getTypeCode(), null, uid)) {
                throw new Exception("已存在相同的编码，请重新编辑！");
            }
            tem.setStatus("1");
            this.iTcrmLossDao.saveOrUpdate(tem);
        } else {

            if (isExist(tem.getCode(), tem.getTypeCode(), tem.getId(), uid)) {
                throw new Exception("已存在相同的编码，请重新编辑！");
            }
//          Map oldMap = this.iTcrmTemplateDao.info(tabName, "id", ref.getId());
            this.iTcrmLossDao.saveOrUpdate(tem);
        }
        //修正下一级编码
        return tem;
    }

    //判断显示编码，显示名称是否重复
    private boolean isExist(String code, String type, String id, String uid) throws Exception {
        String sql = "select * from " + tabName + " where code = ? and company_id=? and type_code = ? ";
        List param = new ArrayList();
        param.add(code);
        param.add(TokenUtil.getCompanyId(uid));
        param.add(type);
        //显示编码和显示名称不可以重复
        if (id != null && id.length() > 0) {
            sql += " and  id != ? ";
            param.add(id);
        }
        List ls = this.iTcrmLossDao.list(sql, param.toArray());
        return ls != null && ls.size() > 0;
    }

    /*
* 根据条件查询，更改
* */
    @Override
    public Map info(String id) throws Exception {
        String sql = "select * from " + tabName + " where id = ? ";
        return this.iTcrmLossDao.info(sql, id);
    }


    @Override
    public Map newPage(String start, String limit, String typeCode, String code, String companyId) throws Exception {
        String sql = "select * from t_crm_car_loss  where company_id = ?  ";

        List<Object> params = new ArrayList();
        params.add(companyId);
        if (!StringUtils.isEmpty(typeCode)) {
            sql += " AND type_code  = ? ";
            params.add(typeCode);

        }
        if (!StringUtils.isEmpty(code)) {
            sql += " AND code like ? or name like ? ";
            params.add("%" + code + "%");
            params.add("%" + code + "%");

        }
        sql += " order by disp_order asc ";
        return this.iTcrmLossDao.page(sql, params.toArray(), start, limit);
    }

    /*
    * 下拉框
    * */
    @Override
    public List<Map> combo(String typeCode, String companyId) throws Exception {
        String sql = "select * from t_crm_car_loss where type_code=? and company_id=? and status=1";
        List<Map<String, Object>> mapList = this.iTcrmLossDao.list(sql, typeCode, companyId);
        List<Map> resList = new ArrayList<Map>();
        Map temp;
        for (Map map : mapList) {
            temp = new HashMap();
            temp.put("label", map.get("code"));
            temp.put("value", map.get("name"));
            resList.add(temp);
        }
        return resList;
    }

    /**
     * 根据参数类型获得公司下的流失方向信息
     * @param typeCode
     * @param uid
     * @return
     * @throws Exception
     */
    @Override
    public List<Map<String, Object>> getLossListByTypeCode(String typeCode, String uid,String companyId) throws Exception {
        if("".equals(companyId)) {
            companyId = TokenUtil.getCompanyId(uid);
        }
        String sql="select * from t_crm_car_loss where type_code=? and company_id=? and status=1";
        return this.iTcrmLossDao.list(sql,typeCode,companyId);
    }

}
